package com.example.treebeard.mytoast;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button btnshow1;
    Button btnshow2;
    Button btnshow3;
    Button btnshow4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnshow1 = (Button) findViewById(R.id.btnShow);
        btnshow1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, "测试Toast弹出消息框", Toast.LENGTH_SHORT);
                toast.show();
            }
        });


        btnshow2 = (Button) findViewById(R.id.btnShow2);
        btnshow2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, "更改Toast弹出位置", Toast.LENGTH_SHORT);
                //第一个参数设置Toast的相对位置，第二个参数为x坐标（正为右，负为左），第三个参数为y坐标(正为下，负为上)
                toast.setGravity(Gravity.CENTER, 0, -250);
                toast.show();
            }
        });


        btnshow3 = (Button) findViewById(R.id.btnShow3);
        btnshow3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, "< 20165316 孙勖哲 >", Toast.LENGTH_SHORT);
                //获取toast的布局方式
                LinearLayout toast_layout = (LinearLayout) toast.getView();
                ImageView iv = new ImageView(MainActivity.this);   //创建图片控件
                iv.setImageResource(R.mipmap.ic_launcher);  //设置显示图片
//                toast_layout.addView(iv);   //添加图片控件到toast布局中，默认添加到末尾
                toast_layout.addView(iv, 0);   //添加图片控件到toast布局中，添加到文字之前
                toast.show();  //显示图片
            }
        });

        btnshow4 = (Button) findViewById(R.id.btnShow4);
        btnshow4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取inflater对象，用来加载视图
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                View toast_view = inflater.inflate(R.layout.toast_layout,null);
                Toast toast = new Toast(MainActivity.this);  //创建一个Toast
                toast.setView(toast_view);  //设置Toast的视图
                toast.show();   //显示
            }
        });
    }
}